<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">添加内容
        </el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="contentInfoList">
      <el-table-column label="标题" align="center" prop="contentTitle" />
      <el-table-column label="顺序" align="center" prop="weight" sortable="" />
      <el-table-column label="类型" align="center" prop="contentType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.content_type" :value="scope.row.contentType" />
        </template>
      </el-table-column>
      <el-table-column label="封面" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="输入的手机号/用户名" align="center" prop="userName"/>-->
      <el-table-column label="发布者昵称" align="center" disabled="disabled" prop="publishNick" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['system:bookinfo:edit']"
            @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="removeContentInfo(scope.row)">移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 修改 -->
    <el-dialog :title='title' :visible.sync="opens" width="50%" :before-close="handleClose">

      <!-- <span>这是一段信息</span> -->
      <el-form :model="queryParams" ref="queryParams" size="small" :inline="true" label-width="100px">
        <!-- <el-form-item label="名称" prop="musicName">
    <el-input v-model="queryParams.musicName" clearable />
  </el-form-item> -->
        <el-form-item label="顺序" prop="weight">
          <el-input v-model="queryParams.weight" clearable />
        </el-form-item>
        <!-- <el-form-item label="音乐家" prop="musicMaker">
    <el-input v-model="queryParams.musicMaker" clearable />
  </el-form-item> -->

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="800px" append-to-body>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="标题" prop="contentTitle">
          <el-input v-model="queryParams.contentTitle" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="musicChooseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" />
        <el-table-column label="标题" align="center" prop="contentTitle" />
        <el-table-column label="顺序" align="center" prop="weight" sortable="" />
        <!--      <el-table-column label="封面" align="center" prop="imgUrl" />-->
        <el-table-column label="封面" align="center" prop="img" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.img" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="发布者昵称" align="center" prop="publishNick" />
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
      <el-divider></el-divider>
      <el-button type="primary" @click="addToCategoryAction">添加到模块</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { addToCategory, listContentsByCategory, removeFromCategory,updateToCategory } from "../../../api/system/contentCategory";
import { listContentinfo } from "../../../api/system/contentinfo";

export default {
  name: "contentInfoList",
  dicts: ['content_type'],

  data() {
    return {
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      musicChooseList: [],
      contentInfoList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contentTitle: null,
        weight: null,
        contentId:null
      },
      loading: false,
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      total: 0,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      form: {}
    }
  },
  created() {
    const categoryId = this.$route.params && this.$route.params.categoryId;
    this.categoryId = categoryId;
    this.getContentList(categoryId)
  },
  methods: {

    // 取消按钮
    cancel() {
      this.open = false;
      this.opens = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        categoryId: null,
        contentTitle: "",
        contentType: null,
        id: null,
        img: "",
        publishNick: "",
        userName: "",
        weight: null,
        contentId:null
      };
    },

    getContentList(categoryId) {
      listContentsByCategory(categoryId).then(res => {
        
        this.contentInfoList = res.data
        console.log('====================================');
        console.log(res.data);
        console.log('====================================');
      })
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加内容到该模块";
      this.getList()
    },
    addToCategoryAction() {
      const ids = this.ids;
      console.log(ids)
      addToCategory(ids, this.categoryId).then(res => {
        if (res.code == 200) {
          this.open = false
          this.getContentList(this.categoryId)
        }
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // 显示修改弹窗
      // this.dialogVisible = true
      this.opens = true;

      const id = row.contentId || this.id
      listContentsByCategory(id).then(response => {
        this.form = response.data;
        this.opens = true;
        this.title = "修改顺序";
      })
      // 获取当前行的musicName、musicIndex、musicMaker值
      // this.queryParams.musicName = row.musicName
      // this.queryParams.musicIndex = row.musicIndex
      this.queryParams.id = row.contentId
      this.queryParams.weight = row.weight

    },
    /** 提交按钮 */
    submitForm() {
      // 关闭弹窗
      // this.dialogVisible = false;
      // 校验表单
      this.$refs["queryParams"].validate(valid => {
        if (valid) {
          if (this.queryParams.id != null) {
            updateToCategory(this.queryParams).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.opens = false;
                this.getList();
                
            });
            
          }
        }
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    removeContentInfo(row) {
      
      if (row.contentId === undefined) {
        return
      }
      removeFromCategory(row.contentId).then(res => {
        if (res.code == 200) {
          this.getContentList(this.categoryId)
          this.getList()
        }
      })
    },
    getList() {
      listContentinfo(this.queryParams).then(res => {
       
        this.musicChooseList = res.rows
        this.total = res.total
      })
    },
    reset() {

    }

  }
}
</script>

<style scoped></style>
